//package com.rainng.coursesystem.base.filter;
//
//import com.rainng.coursesystem.base.utils.jwt.JwtUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@Order(1)
//@WebFilter(filterName = "JWTFilter",urlPatterns = {"/*"})
//@Component
//@Slf4j
//public class JwtFilter implements Filter {
//    private static final String TOKEN = "token";
//    private static final String LOGIN_URL = "/sys/user/login";
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        HttpServletRequest req = (HttpServletRequest) request;
//        HttpServletResponse res = (HttpServletResponse) response;
//        String uri = req.getRequestURI();
//
//        if (uri.endsWith(LOGIN_URL)){
//            chain.doFilter(request,response);
//            return;
//        }
//        String token = req.getHeader(TOKEN);
//        if (StringUtils.isBlank(token)){
//            res.sendError(HttpServletResponse.SC_UNAUTHORIZED,"未授权，请先登录！");
//            return;
//        }
//        log.info("=====token==={}",token);
//        String username = null;
//        try {
//            username = JwtUtils.getUsername(token);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        if (!JwtUtils.verify(token,username)){
//            log.info("----token----校验不通过-----{}",token);
//            res.sendError(HttpServletResponse.SC_UNAUTHORIZED,"token解析错误，请先登录！");
//            return;
//        }
//        req.setAttribute("username",username);
//        chain.doFilter(request,response);
//    }
//}
